package com.robusta.cityuate.fragments;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.robusta.cityuate.R;
import com.robusta.cityuate.activities.SpotProfileActivity;
import com.robusta.cityuate.base.ViewType;
import com.robusta.cityuate.objects.Spot;

public class SpotsMapFragment extends SupportMapFragment implements
		OnMarkerClickListener, InfoWindowAdapter, OnInfoWindowClickListener {

	private HashMap<String, Spot> markerSpotsMap = new HashMap<String, Spot>();
	private View mWindowBubble;

	public SpotsMapFragment() {
		super();

	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		container =(LinearLayout)inflater.inflate(R.layout.custom_map_fragment_view, container);
		
		View v = super.onCreateView( inflater,  container, savedInstanceState);
		container.addView(v);
		return container;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		getMap().setOnMarkerClickListener(this);
		getMap().setInfoWindowAdapter(this);
		getMap().setOnInfoWindowClickListener(this);
		mWindowBubble = getActivity().getLayoutInflater().inflate(
				R.layout.spot_bubble_window, null);
	}

	public Marker addMarker(Spot spot) {
		LatLng loc = new LatLng(spot.getLat(), spot.getLongt());
		
		Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
		Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker).copy(conf, true);
		
		 Canvas c = new Canvas(bmp);
		    Paint p = new Paint();
		    p.setTextSize(15);
		    p.setTextAlign(Paint.Align.CENTER);
		    c.drawText("10$",bmp.getHeight()/2 ,bmp.getWidth()/2, p);
		//canvas.drawText("10.0$", 0, 50, paint);
		Marker spotMarker = getMap().addMarker(
				new MarkerOptions()
						.position(loc)
						.title(spot.getName())
						.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
		markerSpotsMap.put(spotMarker.getId(), spot);
		return spotMarker;
	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		return false;
	}

	@Override
	public View getInfoContents(Marker marker) {
		return null;
	}

	@Override
	public View getInfoWindow(Marker marker) {
		renderBubble(marker, mWindowBubble);
		return mWindowBubble;
	}

	/**
	 * render the bubble with needed information
	 * 
	 * @param marker
	 * @param view
	 */
	private void renderBubble(Marker marker, View view) {

		Spot spot = markerSpotsMap.get(marker.getId());
		TextView titleUi = ((TextView) view.findViewById(R.id.title));
		titleUi.setText(spot.getName());
		TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
		snippetUi.setText(spot.getAddress());
	}

	@Override
	public void onInfoWindowClick(Marker marker) {
		Spot spot = markerSpotsMap.get(marker.getId());
		Intent offering = new Intent(getActivity(), SpotProfileActivity.class);
		offering.putExtra(Spot.SPOT, spot);
		
		offering.putExtra(ViewType.VIEWTYPE, ViewType.Type.RENTER);
		getActivity().startActivity(offering);

	}

}
